import {
	BASE_URL
} from "@/api/url.js"

export function request(config = {}) {
	let {
		url,
		data = {},
		method = "post",
		header = {}
	} = config

	url = BASE_URL + url
	// url = "/api" + url
	if (url.indexOf('login') == -1) {
		header['X-Access-Token'] = uni.getStorageSync('token') || ''
	}

	return new Promise((resolve, reject) => {
		uni.request({
			url,
			data,
			method,
			header,
			success: res => {
				console.log('返回参数',res);
				if (res.data.code == 200) {
					resolve(res.data)
				} else if (res.data.code == 401) {
					uni.showToast({
						title: '登录过期,请重新登录',
						icon: "none",
						duration: 1500,
					})
					setTimeout(() => {
						uni.redirectTo({
							url: '/pages/login/login'
						})
					}, 1500)
					reject(res.data)
				} else if (res.data.code == 50001 || res.data.code == 50002) {
					// 使用后端提示信息
					uni.showToast({
						title: res.data.message,
						icon: "none"
					})
					reject(res.data)
				} else if (res.data.code == 10060) {
					console.log(res);
					reject(res.data)
				} else {
					uni.showToast({
						title: '请求数据失败，请稍后再试',
						icon: "none"
					})
					reject(res.data)
				}
			},
			fail: err => {
				reject(err)
			}
		})
	})
}